package com.zt.business.service;

import com.zt.business.vo.ModuleMenu;
import com.zt.model.entity.Module;
import com.zt.model.mapper.ModuleMapper;

import java.util.ArrayList;
import java.util.List;


public class ModuleService extends BaseSerivce<ModuleMapper> {


    /**
     * 组装初始化菜单数据
     * @return
     */
    public List<ModuleMenu> initMenu(){
        List<ModuleMenu> rootMenu = new ArrayList<>();
        instance(ModuleMapper.class);

        //查询父级的所有菜单 组装父级查询条件
        Module queryInfo = new Module();
        queryInfo.setLevel("1");
        //查询所有的父级菜单
        List<Module> menu = mapper.select(queryInfo);

        //把Module中的数组组装为前端所需要的格式ModuleMenu
        ModuleMenu moduleMenu = null;
        List<ModuleMenu> children;
        for(Module m : menu){
            children = new ArrayList<>();
            moduleMenu = new ModuleMenu(m.getName(),m.getIcon(),m.getUrl(),m.getSpread()==1,children);
            rootMenu.add(moduleMenu);

            //根据父ID 查询对应的子级信息
            queryInfo.setLevel("2");
            queryInfo.setPid(m.getId());
            List<Module> childMenu = mapper.select(queryInfo);
            for(Module child : childMenu){
                moduleMenu = new ModuleMenu(child.getName(),child.getIcon(),child.getUrl(),child.getSpread()==1,null);
                children.add(moduleMenu);
            }
        }
        return rootMenu;
    }


    public int insert(Module pojo){
        instance(ModuleMapper.class);
        return mapper.insert(pojo);
    }

    public int insertList(List< Module> pojos){
        instance(ModuleMapper.class);
        return mapper.insertList(pojos);
    }

    public List<Module> select(Module pojo){
        instance(ModuleMapper.class);
        return mapper.select(pojo);
    }

    public int update(Module pojo)
    {
        instance(ModuleMapper.class);
        return mapper.update(pojo);
    }

}
